<template>
  <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <van-cell v-for="item in list" :key="item.id">
      <knowledge-item :knowledge="item"></knowledge-item>
    </van-cell>
  </van-list>
</template>

<script lang="ts" setup>
import { ApiRes } from '@/types/data'
import { Knowledges, Knowledge } from '@/types/knowledge'
import request from '@/utils/request'
import KnowledgeItem from './knowledge-item.vue'
import { ref } from 'vue'
const props = defineProps<{ type: string }>()
const finished = ref(false)
const loading = ref(true)
const list = ref([] as Knowledge[])
const current = ref(0)
const pageSize = ref(10)
// 加载数据（第一次以及上拉加载更多都会使用到它）
const onLoad = async () => {
  current.value++
  const res = await request.get<ApiRes<Knowledges>>('patient/home/knowledge', {
    params: {
      type: props.type,
      current: current.value,
      pageSize: pageSize.value
    }
  })

  if (res.data.code === 10000) {
    loading.value = false
    list.value = [...list.value, ...res.data.data.rows]
    if (res.data.data.rows.length === 0) {
      finished.value = true
    }
  }
}
</script>
